package com.maxwell.googleplay;

import com.astuetz.PagerSlidingTabStrip;
import com.astuetz.PagerSlidingTabStripExtends;
import com.maxwell.googleplay.base.BaseActivity;
import com.maxwell.googleplay.base.BaseFragment;
import com.maxwell.googleplay.base.MyViewPager;
import com.maxwell.googleplay.factory.FragmentFactory;
import com.maxwell.googleplay.utils.LogUtils;
import com.maxwell.googleplay.utils.UIUtils;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import android.support.v4.app.FragmentStatePagerAdapter;

import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends BaseActivity {

	private PagerSlidingTabStripExtends mTabs;
	private ViewPager mViewPager;
	private String[] mMainTitles;
	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle mToggle;

	
	@Override
	public void initView() {
		setContentView(R.layout.activity_main);
		mTabs = (PagerSlidingTabStripExtends) findViewById(R.id.main_tabs);
		mViewPager = (ViewPager) findViewById(R.id.main_viewpager);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawerlayout);

	}

	@Override
	public void initActionBar() {
		//
		ActionBar actionBar = getSupportActionBar();
		actionBar.setLogo(R.drawable.ic_launcher);
		actionBar.setTitle("GooglePlay");

		// 显示返回按钮
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);

		initActionBarToggle();
	}

	private void initActionBarToggle() {
		mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer_am, R.string.open, R.string.close);

		// 同步状态
		mToggle.syncState();
		// 设置拖动监听
		mDrawerLayout.setDrawerListener(mToggle);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// mToggle控制打开关闭菜单
			mToggle.onOptionsItemSelected(item);
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void initData() {

		mMainTitles = UIUtils.getStringArr(R.array.main_titles);

		// mViewPager.setAdapter(new MainPagerAdapter());

		/*
		 * MainFragmentPagerAdapter adapter = new MainFragmentPagerAdapter(
		 * getSupportFragmentManager());
		 */
		MainfragmentStatePagerAdapter adapter = new MainfragmentStatePagerAdapter(
				getSupportFragmentManager());
		mViewPager.setAdapter(adapter);
		// 绑定tabs到viewpager
		mTabs.setViewPager(mViewPager);

	}

	@Override
	public void initListener() {

		mTabs.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// 完成触发加载
				BaseFragment fragment = FragmentFactory.getFragment(position);
				if (fragment != null) {
					fragment.getLoadingPager().loadData();
				}
			}

			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub

			}
		});

	}

	/*
	 * Initialize the contents of the Activity's standard options menu. You
	 * should place your menu items in to menu.
	 * 
	 * @Returns:You must return true for the menu to be displayed; if you return
	 * false it will not be shown.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	class MainfragmentStatePagerAdapter extends FragmentStatePagerAdapter {

		public MainfragmentStatePagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = FragmentFactory.getFragment(position);
			LogUtils.sf("初始化了：" + mMainTitles[position]);
			return fragment;
		}

		@Override
		public int getCount() {
			if (mMainTitles != null) {
				return mMainTitles.length;
			}
			return 0;
		}

		@Override
		public CharSequence getPageTitle(int position) {

			return mMainTitles[position];
		}
	}

	/*
	 * class MainPagerAdapter extends PagerAdapter {
	 * 
	 * @Override public int getCount() { if (mMainTitles != null) { return
	 * mMainTitles.length; } return 0; }
	 * 
	 * @Override public boolean isViewFromObject(View view, Object object) {
	 * 
	 * return view == object; }
	 * 
	 * @Override public void destroyItem(ViewGroup container, int position,
	 * Object object) { container.removeView((View) object); }
	 * 
	 * @Override public Object instantiateItem(ViewGroup container, int
	 * position) {
	 * 
	 * TextView tv = new TextView(UIUtils.getContext());
	 * tv.setText(mMainTitles[position]); container.addView(tv); return tv; }
	 * 
	 * // 必须重写此方法，返回tabs值
	 * 
	 * @Override public CharSequence getPageTitle(int position) {
	 * 
	 * return mMainTitles[position]; }
	 * 
	 * }
	 */
	/*
	 * class MainFragmentPagerAdapter extends FragmentPagerAdapter {
	 * 
	 * public MainFragmentPagerAdapter(FragmentManager fm) { super(fm); // TODO
	 * Auto-generated constructor stub }
	 * 
	 * @Override public Fragment getItem(int position) { // 创建Fragment Fragment
	 * fragment = FragmentFactory.getFragment(position);
	 * 
	 * LogUtils.sf("初始化" + mMainTitles[position]);
	 * 
	 * return fragment; }
	 * 
	 * @Override public int getCount() { if (mMainTitles != null) { return
	 * mMainTitles.length; } return 0; }
	 * 
	 * @Override public CharSequence getPageTitle(int position) {
	 * 
	 * return mMainTitles[position]; }
	 * 
	 * }
	 */

}
